import { CARTS, COLLECTIONS, FOOTPRINTS } from "../../config";
import { getDetall } from "../../request/goods";
import { Event } from "../../utils/interface";

interface Attr {
  add_price: number;
  attr_id: number;
  attr_name: string;
  attr_sel: string;
  attr_vals: string;
  attr_value: string;
  attr_write: string;
  goods_id: number;
}

export interface Detail {
  add_time: number;
  attrs: Array<Attr>;
  cat_id: number;
  cat_one_id: number;
  cat_three_id: number;
  cat_two_id: number;
  delete_time: string | null;
  goods_big_logo: string;
  goods_cat: string;
  goods_id: number;
  goods_introduce: string;
  goods_name: string;
  goods_number: number;
  goods_price: number;
  goods_small_logo: string;
  goods_state: number;
  goods_weight: string;
  hot_mumber: number;
  is_del: string | number;
  is_promote: boolean;
  pics: Array<{ pics_big: string }>;
  upd_time: number;
  count: number;
  flag: boolean;
}
interface Row {
  icon: string;
  text: string;
}

let navigatorBer: Array<Row> = [
  { text: "联系客服", icon: "icon-pinglun" },
  { text: "分享", icon: "icon-fenxiang" },
  { text: "购物车", icon: "icon-icon_fabu" },
];

let detail: Detail | {} = {};
let goods_id = 0;
let isCol = false;
let isCart = false;
Page({
  data: {
    detail,
    navigatorBer,
    isCol,
    isCart,
  },
  onLoad(e) {
    goods_id = (e.goods_id as unknown as number) || 57420;
    this.initDetail();
    this.addFoo();
  },
  addFoo() {
    let foo = wx.getStorageSync(FOOTPRINTS) as Array<Detail>;
    if (!Array.isArray(foo)) foo = [];
    const flag = foo.some(
      (item) => item.goods_id === (detail as Detail).goods_id
    );
    if (!flag) {
      foo.push(detail as Detail);
      wx.setStorageSync(FOOTPRINTS, foo);
    }
  },
  async initDetail() {
    const result = await getDetall(goods_id);
    detail = result;
    isCart = this.isCart();
    isCol = this.isCollection();
    this.setData({ detail, isCart, isCol });
  },
  collection() {
    let collections = wx.getStorageSync("collections");
    let i = -1;
    if (!Array.isArray(collections)) {
      collections = [];
    }
    i = collections.findIndex(
      (item: any) => item.goods_id === (detail as Detail).goods_id
    );
    if (i !== -1) {
      wx.setStorageSync(
        COLLECTIONS,
        collections.filter((_: any, index: number) => i !== index)
      );
      isCol = false;
      this.setData({ isCol });
      return wx.showToast({
        title: "取消收藏成功",
        icon: "error",
        duration: 1500,
      });
    }
    collections.push(detail as Detail);
    wx.setStorageSync("collections", collections);
    wx.showToast({
      title: "收藏成功",
      icon: "success",
      duration: 1500,
    });
    isCol = true;
    this.setData({ isCol });
  },
  isCollection() {
    let collection = wx.getStorageSync(COLLECTIONS) as Array<Detail>;
    if (!Array.isArray(collection)) return false;
    let is = collection.some(
      (item) => item.goods_id === (detail as Detail).goods_id
    );
    return is;
  },
  link(e: Event<{ row: Row }>) {
    const str = e.currentTarget.dataset.row.text;
    if (str === "联系客服") {
      wx.showToast({
        title: "当前没有客服",
        icon: "error",
        duration: 1500,
      });
    } else if (str === "分享") {
      wx.updateShareMenu({
        withShareTicket: true,
        success() {
          wx.showToast({
            title: "分享成功",
            icon: "success",
            duration: 1500,
          });
        },
        fail() {
          wx.showToast({
            title: "分享失败",
            icon: "none",
            duration: 1500,
          });
        },
      });
    } else {
      wx.switchTab({
        url: "/pages/cart/cart",
      });
    }
  },
  clickImg(e: Event<{ urls: string }>) {
    detail;
    const urls: string[] = [];
    (detail as Detail).pics.forEach((item) => {
      urls.push(item.pics_big);
    });
    wx.previewImage({
      current: e.currentTarget.dataset.urls, // 当前显示图片的http链接
      urls, // 需要预览的图片http链接列表
    });
  },
  addCart() {
    let carts = wx.getStorageSync(CARTS) as Array<Detail>;
    if (!Array.isArray(carts)) carts = [];
    const flag = carts.some(
      (item) => item.goods_id === (detail as Detail).goods_id
    );

    if (!flag) {
      (detail as Detail).count = 1;
      (detail as Detail).flag = true;
      carts.push(detail as Detail);
      wx.setStorageSync(CARTS, carts);
    }
    isCart = true;
    this.setData({ isCart });
    wx.showToast({
      title: flag ? "商品已在购物车中" : "加入成功",
      icon: "success",
      duration: 1000,
    });
  },
  isCart() {
    let carts = wx.getStorageSync(CARTS) as Array<Detail>;
    if (!Array.isArray(carts)) return false;
    return carts.some((item) => item.goods_id === (detail as Detail).goods_id);
  },
  toPay() {
    const del = detail as Detail;
    del.goods_introduce = "";
    del.count = 1;
    wx.navigateTo({
      url: "/pages/pay/pay?row=" + JSON.stringify(del),
    });
  },
});
